/*
 * 文件名：LocalRotaServiceImpl.java	 
 * 时     间：下午3:36:32
 * 作     者：Administrator       
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.local.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.local.dao.mapers.LocalRotaMapper;
import com.local.dao.mapers.LocalRotaUserMapper;
import com.local.model.LocalRota;
import com.local.model.LocalRotaExample;
import com.local.model.LocalRotaUser;
import com.local.page.LocalRotaPage;
import com.local.service.face.LocalRotaService;
import com.qianhai.common.dao.face.BaseMapper;
import com.qianhai.common.exception.BaseException;
import com.qianhai.common.service.impl.BaseServiceImpl;
import com.qianhai.common.utils.Tools;
import com.qianhai.common.utils.UUIDGenerateUtil;

import common.cz0735.coustomer.entity.pages.CoustomerInfoPage;
import common.cz0735.coustomer.mapers.CoustomerInfoMapper;
import common.cz0735.coustomer.model.CoustomerInfo;

/** 
 * 类描述:这里是类描述
 * @ClassName: LocalRotaServiceImpl 
 * @author Administrator
 * @date 2015年10月3日 下午3:36:32 
 */
@Service
public class LocalRotaServiceImpl extends
BaseServiceImpl<LocalRotaExample, LocalRota, String> implements LocalRotaService
{
	@Autowired
	private LocalRotaMapper localRotaMapper;
	
	@Autowired
	private LocalRotaUserMapper localRotaUserMapper;
	
	@Autowired
	private CoustomerInfoMapper coustomerInfoMapper;
	
	@Override
	public BaseMapper<LocalRotaExample, LocalRota, String> getMapper()
	{
		// TODO Auto-generated method stub
		return localRotaMapper;
	}

	@Override
	public List<LocalRota> queryRotaByPage(LocalRotaPage<LocalRota> page)
	{
		// TODO Auto-generated method stub
		Integer recordCount = localRotaMapper.countAllByPage(page);
		page.setTotalRecords(recordCount);
		List<LocalRota> list = localRotaMapper.queryByPage(page);
		page.setData(list);
		return list;
	}

	@Override
	public String saveOrUpdateLocalRota(LocalRota localRota)
	{
		// TODO Auto-generated method stub
		try
		{
			if(!Tools.isEmpty(localRotaMapper.findRotaByRotaName(localRota.getRotaName()))){
				throw new BaseException("15000-00000-00025");
			}
			
			if (Tools.isEmpty(localRota.getUuid()))
			{
				localRota.setUuid(UUIDGenerateUtil.generate());
				localRotaMapper.insert(localRota);
			} else
			{
				localRotaMapper.updateByPrimaryKey(localRota);
			}
		} catch (Throwable e)
		{
			throw new BaseException(e.getMessage());
		}
		return localRota.getUuid();
	}

	@Override
	public List<LocalRota> queryRotaUser(LocalRotaPage<LocalRota> page)
	{
		// TODO Auto-generated method stub
		List<LocalRota> li=localRotaMapper.queryRotaUser(page);
		return li;
	}

	@Override
	public List<LocalRota> queryRotaUserOther(LocalRotaPage<LocalRota> page)
	{
		CoustomerInfoPage coustomerInfoPage = new CoustomerInfoPage();
		List<CoustomerInfo> list = coustomerInfoMapper.queryByPage(coustomerInfoPage);
		List<LocalRota> li = new ArrayList<LocalRota>();
		for (CoustomerInfo coustomerInfo : list) {
			LocalRota localRota = new LocalRota();
			localRota.setUserId(coustomerInfo.getUuid());
			localRota.setUserName(coustomerInfo.getName());
			localRota.setLoginUserId(coustomerInfo.getLoginUserUuid());
			li.add(localRota);
			
		}
		return li;
	}

	@Override
	public void deleteByIdAndRotaUser(String uuid)
	{
		// TODO Auto-generated method stub
		try{
			List<LocalRotaUser> list=localRotaUserMapper.queryByRotaUuid(uuid);
			for(LocalRotaUser lr:list){
				localRotaUserMapper.deleteByPrimaryKey(lr.getUuid());
			}
			localRotaMapper.deleteByPrimaryKey(uuid);
		}catch (Throwable e)
		{
			throw new BaseException(e.getMessage());
		}
	}
	
	
	public LocalRota queryLocalRotaByUserId(LocalRota localrota){
		return localRotaMapper.queryLocalRotaByUserId(localrota);
	}

	@Override
	public LocalRota queryRotaByStationAndUser(String stationUuid, String uuid)
	{
		// TODO Auto-generated method stub
		try
		{
			LocalRota lr=localRotaMapper.queryRotaByStationAndUser(stationUuid, uuid);
			return lr;
		} catch (Exception e)
		{
			// TODO: handle exception
			return null;
		}
	}

}
